var currentModel;
var line;
var table;
var currentRatioIndex;

function changeTapRatio(){
	currentRatioIndex = $('option:selected').val();
	var currentData = currentModel[currentRatioIndex];
	loadDetailTable(currentData.averageShortReactance);
	loadDetailChart(currentData);
}
function hideWaveView(){
	$('#rightBody').css('display','none');
	$('#selectRatiosDiv').css('display','none');
}
function initWaveView(){
	var typeEle = document.getElementById('form:currentNodeType');
	$('#rightTitle').html();
	if(typeEle.value == "time"){
		var jsonEle = document.getElementById('form:currentChartJson');
		currentModel = eval('('+jsonEle.value+')');
		$('#rightBody').css('display','block');
		$('#selectRatiosDiv').css('display','block');
		loadSelectDiv(currentModel);
		var currentData = currentModel[currentRatioIndex];
		$('#rightTitle').html(currentData.stationName+' '+currentData.transformName+'变压器  '+currentData.faultTime);
		loadDetailTable(currentData.averageShortReactance);
		loadDetailChart(currentData);
	}else{
		hideWaveView();
	}
}
function loadSelectDiv(data){
	var div = $('#selectRatios');
	div.html('');
	var currentRatio = 11;
	for(var i in data){
		var selected = '';
		var ratio = Number(data[i].tapRatio);
		if(ratio == 0){
			selected = ' selected="selected" ';
		}
		if(Math.abs(ratio)<Math.abs(currentRatio)){
			currentRatioIndex = i;
			currentRatio = ratio;
		}
		var n = ratio*100+"%";
		var select = '<option '+selected+' label="'+n+'" value="'+i+'"></option>';
		div.append(select);
	}
}
function loadDetailTable(averages){
	averages = eval('('+averages+')');
	var table = $('#detailTable');
	table.html('');
	var j = 0;
	var tableData = {
			XA12:null,
			XA13:null,
			XA23:null,
			XB12:null,
			XB13:null,
			XB23:null,
			XC12:null,
			XC13:null,
			XC23:null
	};
	//对abc进行排序
	for(var item in averages){
		tableData[item] = averages[item];
	}
	var tr = $('<tr></tr>');
	for(var item in tableData){
		tr.append('<td>'+getName(item)+'</td>');
		var selectClass = '';
		var ratioStr = '<span style="color:gray">空</span>';
		if(tableData[item] || tableData[item] == 0){
			var ratio = ((tableData[item])*100).toFixed(2);
			ratioStr = ratio+"%";
			if(ratio>10){
				selectClass = 'tdanger';
			}else if(ratio>5){
				selectClass = 'twarning';
			}
		}
		tr.append('<td class="'+selectClass+'"><span>'+ratioStr+'</span></td>');
		j++;
		if(j == 3){
			table.append(tr);
			j = 0;
			tr = $('<tr></tr>');
		}
	}
}
function loadDetailChart(currentData){
	//折线图
	var line = echarts.init(document.getElementById("detailChart"));;
	var dataMap = new Map();
	for(var i in currentData){
		var key;
		if(i.indexOf("pre")>-1){
			key = i.substring(17,i.length);
			dataMap.put(-1*Number(key),eval('('+currentData[i]+')'));
		}else if(i.indexOf("post")>-1){
			key = i.substring(18,i.length);
			dataMap.put(Number(key),eval('('+currentData[i]+')'));
		}else{
			continue;
		}
	}
	var seriesDatas = {
	                   XA12:[],
	                   XA13:[],
	                   XA23:[],
	                   XB12:[],
	                   XB13:[],
	                   XB23:[],
	                   XC12:[],
	                   XC13:[],
	                   XC23:[]
	};
	var size = dataMap.size();
	dataMap.each(function(index,key,value){
		for(var i in seriesDatas){
			seriesDatas[i].push([key,Number(value[i])]);
		}
	});
	var series = [];
	var legend = [];
	var legendSelected = {};
	for(var i in seriesDatas){
		var name = getName(i);
		if(name.indexOf("高中")>-1){
			legendSelected[name] = true;
		}else{
			legendSelected[name] = false;
		}
		series.push({
			name:name,
			type:'line',
			connectNulls:false,
			clipOverflow:true,
			smooth:true,
			symbolSize:0,
			data:seriesDatas[i].sort(sortData)
		});
		legend.push({name:name});
	}
	var option = {
			 color:['yellow','yellow', 'yellow','green', 'green','green' ,'red','red', 'red'],
			 tooltip : {
			        trigger: 'axis',
			        axisPointer: {
			            type: 'cross',
			            animation: false,
			            label: {
			                backgroundColor: '#505765'
			            }
			        }
			},
			legend:{
				data:legend,
				selected:legendSelected,
			},
			dataZoom:[{
				type:'inside',
				yAxisIndex:0
			}],
			toolbox:{
				feature:{
					saveAsImage:{
						show:true
					},
					dataZoom:{show:true},
					restore:{show:true}
				}
			},
			xAxis:{
				type:'value',
				name:'时间/ms',
				axisLabel:{
					formatter: '{value} ms'
				}
			},
			yAxis:{
				type:'value',
				name:'电抗/Ω',
				axisLabel:{
					formatter: '{value} Ω'
				}
			},
			textStyle:{
				fontSize:14
			},
			series:series
	};
	line.setOption(option);
	line.on("legendselectchanged",function(params){
		var status = params.selected[params.name];
		var common = params.name.substring(1,params.name.length);
		var selected = params.selected;
		for(var item in selected){
			if(item.indexOf(common)>-1){
				selected[item] = status;
			}else{
				selected[item] = !status;
			}
		}
		var option = {legend:{selected:selected}};
		line.setOption(option);
	});
}
function sortData(obj1,obj2){
	return obj1[0]-obj2[0];
}
function getName(item){
	name = '';
	var items = item.split("");
	if(items && items.length>0){
		name += items[1]+"相";
		name += (items[2] == 1?"高":"中");
		name += (items[3] == 2?"中":"低");
		name += "压侧";
	}
	return name;
}